package com.fanli.bigdata.db

import com.redis.RedisClient

class StateRedisConnections(val host: String, val port: Int = 6379) {
  val redis = new RedisClient(host, port)
}

class RedisCons(val host: String, val port: Int = 6379) {
  RedisCons.redis_host = host
  RedisCons.redis_port = port

  def getInstance(): RedisClient = {
    if (RedisCons.redisCli == null) {
      println("rhost=" + RedisCons.redis_host + " , rport=" + RedisCons.redis_port)
      RedisCons.redisCli = new RedisClient(RedisCons.redis_host, RedisCons.redis_port)
    }
    return RedisCons.redisCli
  }
}

object RedisCons {
  val max_ex_redis_get_count = 400
  val min_ex_redis_get_count = 0

  var redis_host: String = null
  var redis_port: Int = 6379
  var redisCli: RedisClient = null

  var getVarnishExCount: Int = -1
  var getApacheExCount: Int = -1

  var varnishExList = List[(String, String)]()
  var apacheExList = List[(String, String, String)]()

  def apply(host: String, port: Int) = new RedisCons(host, port)

  /**
   * 判断是否需要获取varnish过滤规则
   */
  def needGetVarnishEx(): Boolean = {
    getVarnishExCount < min_ex_redis_get_count || getVarnishExCount > max_ex_redis_get_count
  }

  /**
   * 判断是否需要获取apache过滤规则
   */
  def needGetApacheEx(): Boolean = {
    getApacheExCount < min_ex_redis_get_count || getApacheExCount > max_ex_redis_get_count
  }

  /**
   * redis获取varnish过滤规则计数增加
   */
  def addGetVarnishExCounter() = {
    getVarnishExCount = getVarnishExCount + 1
  }
  /**
   * redis获取varnish过滤规则计数清零
   */
  def clearGetVarnishExCounter() = {
    getVarnishExCount = min_ex_redis_get_count
  }
  /**
   * redis获取apache过滤规则计数增加
   */
  def addGetApacheExCounter() = {
    getApacheExCount = getApacheExCount + 1
  }
  /**
   * redis获取apache过滤规则计数增加
   */
  def clearGetApacheExCounter() = {
    getApacheExCount = min_ex_redis_get_count
  }
}